package com.bkgy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bkgy.modular.calsidata.dao.TbDsjC622FlatPassTimeMapper;
import com.bkgy.modular.calsidata.entity.TbDsjC622FlatPassTime;
import com.bkgy.modular.datacollect_bx.bean.*;
import com.bkgy.modular.datacollect_bx.dao.JDataSavemongDao1630;
import com.bkgy.modular.datacollect_bx.dao.JDataSavemongDao1870;
import com.bkgy.modular.datacollect_bx.dao.JDataSavemongDao2150;
import com.bkgy.modular.datacollect_bx.dao.JDataSavemongDao2280;
import com.bkgy.modular.datacollect_bx.service.JDataSaveService621;
import com.bkgy.modular.datacollect_bx.service.JDataSaveService622;
import com.bkgy.modular.datacollect_bx.service.JDataSaveService642;
import com.bkgy.modular.datacollect_bx.service.JDataSaveService653;
import com.bkgy.controller.tbbxrecord.dao.TbBxFileSaveDao;
import com.bkgy.controller.tbbxrecord.entity.TbBxFileSave;
import com.bkgy.modular.tbdsjdatacollect.dao.TbDsjDataCollectDao;
import com.bkgy.modular.tbdsjdatacollect.entity.TbDsjDataCollect;
import com.bkgy.modular.tbdsjruleedit.dao.TbDsjRuleEditDao;
import com.bkgy.modular.tbdsjruleedit.entity.TbDsjRuleEdit;
import com.bkgy.modular.tbdsjruleeditrelation.dao.TbDsjRuleEditRelationDao;
import com.bkgy.modular.tbdsjruleeditrelation.entity.TbDsjRuleEditRelation;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SFtpUtils {

    @Resource
    private RestTemplate restTemplate;

    @Value("${sftp_username}")
    private String username;

    @Value("${sftp_password}")
    private String password;

    @Value("${sftp_port}")
    private Integer port;

    @Autowired
    private JDataSaveService621 jDataSaveService621;

    @Autowired
    private JDataSaveService622 jDataSaveService622;

    @Autowired
    private JDataSaveService642 jDataSaveService642;

    @Autowired
    private JDataSaveService653 jDataSaveService653;

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    private TbDsjDataCollectDao tbDsjDataCollectDao;

    @Resource
    private TbDsjRuleEditDao tbDsjRuleEditDao;

    @Resource
    private CalUtils calUtils;

    @Resource
    private TbDsjRuleEditRelationDao tbDsjRuleEditRelationDao;

    @Resource
    private TbBxFileSaveDao tbBxFileSaveDao;

    @Resource
    private TbDsjC622FlatPassTimeMapper tbDsjC622FlatPassTimeMapper;

    @Resource
    private JDataSavemongDao1630 jDataSavemongDao1630;

    @Resource
    private JDataSavemongDao1870 jDataSavemongDao1870;

    @Resource
    private JDataSavemongDao2150 jDataSavemongDao2150;

    @Resource
    private JDataSavemongDao2280 jDataSavemongDao2280;






    /**
     * 连接sftp服务器
     */
    public ChannelSftp sftpConnect(String host) {
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            Session sshSession = jsch.getSession(username, host, port);//用户名，主机，端口号
            sshSession.setPassword(password);//密码
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.setTimeout(30000); // 设置timeout时间
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            log.info("sftp连接成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            log.error("sftp连接失败");
        }
        return sftp;
    }

    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public void calFile1630(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {
        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 2*1024 * 1024) {
                log.error(directory + downloadFile + "1630文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.info("cal1630开始处理文件");
                //数据采集表查询出621需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "CAL1630");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.findAlias("621",4);
                //单独获取所需的别名
                List<String> aliasList = alias621.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.eq("production_line","CAL1630");
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }

                readFileToMongoDB_new(inputStream1, alias621, tbDsjRuleEdits, "CAL1630");
                log.info("CAL1630文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            /*JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C621");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);*/
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("TCM2280文件处理异常！", e);
            //e.printStackTrace();
        } finally {

        }
    }

    public void calFile1630_TEST(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {
        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 2*1024 * 1024) {
                log.error(directory + downloadFile + "1630文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(4.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.info("cal1630开始处理文件");
                //数据采集表查询出621需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "CAL1630");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.findAlias("621",4);
                //单独获取所需的别名
                List<String> aliasList = alias621.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.eq("production_line","CAL1630");
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }

                readFileToMongoDB_new(inputStream1, alias621, tbDsjRuleEdits, "CAL1630");
                log.info("CAL1630文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            /*JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C621");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);*/
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("TCM2280文件处理异常！", e);
            //e.printStackTrace();
        } finally {

        }
    }


    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public void calFile2280(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {
        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 2*1024 * 1024) {
                log.error(directory + downloadFile + "2280文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.info("TCM2280开始处理文件："+downloadFile);
                //数据采集表查询出621需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "TCM2280");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.findAlias("621",4);
                //单独获取所需的别名
                List<String> aliasList = alias621.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.eq("production_line","TCM2280");
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }

                readFileToMongoDB_new(inputStream1, alias621, tbDsjRuleEdits, "TCM2280");
                log.info("TCM2280文件处理成功："+downloadFile);
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            /*JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C621");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);*/
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("TCM2280文件处理异常！", e);
            //e.printStackTrace();
        } finally {

        }
    }


    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public void calFile2150(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {
        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 2*1024 * 1024) {
                log.error(directory + downloadFile + "2150文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.info("CAL2150开始处理文件");
                //数据采集表查询出621需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "CAL2150");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.findAlias("621",4);
                //单独获取所需的别名
                List<String> aliasList = alias621.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.eq("production_line","CAL2150");
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }

                readFileToMongoDB_new(inputStream1, alias621, tbDsjRuleEdits, "CAL2150");
                log.info("CAL2150文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            /*JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C621");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);*/
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("CAL2150文件处理异常！", e);
            //e.printStackTrace();
        } finally {

        }
    }



    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public void calFile1870(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {
        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 2*1024 * 1024) {
                log.error(directory + downloadFile + "CGL1870文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.info("CGL1870开始处理文件");
                //数据采集表查询出621需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "CGL1870");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.findAlias("621",4);
                //单独获取所需的别名
                List<String> aliasList = alias621.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.eq("production_line","CGL1870");
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }

                readFileToMongoDB_new(inputStream1, alias621, tbDsjRuleEdits, "CGL1870");
                log.info("CGL1870文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            /*JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C621");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);*/
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("CAL2150文件处理异常！", e);
            //e.printStackTrace();
        } finally {

        }
    }




    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public void calFile621(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {
        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 1024 * 1024) {
                log.error(directory + downloadFile + "621文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.error("621开始处理文件");
                //数据采集表查询出621需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "C621");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.findAlias("621",4);
                //单独获取所需的别名
                List<String> aliasList = alias621.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }

                readFileToMongoDB(inputStream1, alias621, tbDsjRuleEdits, "621");
                log.error("621文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C621");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("621文件处理异常！", e);
            //e.printStackTrace();
        } finally {

        }
    }

    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public void calFile622(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {

        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 1024 * 1024) {
                log.error(directory + downloadFile + "622文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.error("622开始处理文件");
                //数据采集表查询出622需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "C622");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias622 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias622 = tbDsjDataCollectDao.findAlias("622",4);
                //单独获取所需的别名
                List<String> aliasList = alias622.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }
                readFileToMongoDB(inputStream1, alias622, tbDsjRuleEdits, "622");
                log.error("622文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C622");
            String jsonString = jsonObject.toJSONString();

            ///先查表，满足flag=2,并且满足flag1=0,发送消息，并且flag1改为1
            LambdaQueryWrapper<TbDsjC622FlatPassTime> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TbDsjC622FlatPassTime::getCoil_id, downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            queryWrapper.eq(TbDsjC622FlatPassTime::getFlag, 2);
            queryWrapper.eq(TbDsjC622FlatPassTime::getFlag1, 0);
            TbDsjC622FlatPassTime tbDsjC622FlatPassTime = tbDsjC622FlatPassTimeMapper.selectOne(queryWrapper);
            if (tbDsjC622FlatPassTime != null) {
                restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
                tbDsjC622FlatPassTime.setFlag1(1);
                tbDsjC622FlatPassTimeMapper.updateById(tbDsjC622FlatPassTime);
            }
            else {
                restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            }

            Thread.sleep(3000);
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("622文件处理异常！", e);
            //e.printStackTrace();
        } finally {

        }
    }

    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public void calFile642(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {

        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 1024 * 1024) {
                log.error(directory + downloadFile + "642文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return;
            }
//            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.error("开始处理文件");
                //数据采集表查询出642需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "C642");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias642 = tbDsjDataCollectDao.selectList(queryWrapper);
//                List<TbDsjDataCollect> alias642 = tbDsjDataCollectDao.findAlias("642",4);
                //单独获取所需的别名
                List<String> aliasList = alias642.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());

                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }


                readFileToMongoDB(inputStream1, alias642, tbDsjRuleEdits, "642");
                log.error("642文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C642");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            log.error("642文件处理异常！", e);

            //e.printStackTrace();
        } finally {

        }
    }

    /**
     * 处理文件
     *
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param host         SFTP主机地址
     * @throws JSchException
     */
    public Boolean calFile653(String directory, String downloadFile, String host, TbBxFileSave tbBxFileSave) throws JSchException {

        ChannelSftp sftp = sftpConnect(host);
        try {
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(directory + downloadFile);
            //判断文件大小
            loadFile(inputStream, "D://TEMP/" + downloadFile);
            File file = new File("D://TEMP/" + downloadFile);
            if (file.length() > 1024 * 1024) {
                log.error(directory + downloadFile + "653文件大小异常---" + host);
                inputStream.close();
                file.delete();
                sftp.disconnect();
                sftp.getSession().disconnect();
                tbBxFileSave.setStatusftp(1.0);
                UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
                tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
                return false;
            }
//            file.delete();
            //读完再读一次
            InputStream inputStream1 = sftp.get(directory + downloadFile);
            if (inputStream1 != null) {
                log.error("653开始处理文件");
                //数据采集表查询出653需要的宝信原始名，和所需的别名
                QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PRODUCTION_LINE", "C653");
                queryWrapper.eq("out_data_source", 1);
                List<TbDsjDataCollect> alias653 = tbDsjDataCollectDao.selectList(queryWrapper);
                //单独获取所需的别名
                List<String> aliasList = alias653.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());

                List<TbDsjRuleEdit> tbDsjRuleEdits = null;
                if (!CollectionUtils.isEmpty(aliasList)) {
                    QueryWrapper<TbDsjRuleEdit> wrapper = new QueryWrapper<>();
                    wrapper.in("dic_variable_name_calc", aliasList);
                    //根据规则表查询出需要计算特征值的变量实体
                    tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapper);
                }


                readFileToMongoDB(inputStream1, alias653, tbDsjRuleEdits, "653");
                log.error("C653文件处理成功");
            }
            inputStream1.close();
            sftp.disconnect();
            sftp.getSession().disconnect();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("volumeNo", downloadFile.substring(0, downloadFile.indexOf(".json.tar")));
            jsonObject.put("line", "C653");
            String jsonString = jsonObject.toJSONString();
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            Thread.sleep(3000);
            return true;
        } catch (Exception e) {
            sftp.disconnect();
            sftp.getSession().disconnect();
            tbBxFileSave.setStatusftp(2.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id, tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave, updateWrapper);
            //log.error("653文件处理异常！", e);
            log.error("653文件处理异常！");

            //e.printStackTrace();
            return false;
        } finally {

        }
    }


    /**
     * 将GZip文件内容写入MongoDB
     *
     * @param inputStream 需要进行json解析的GZip压缩文件流
     */
    //@Async("executor")
    public void readFileToMongoDB_new(InputStream inputStream, List<TbDsjDataCollect> alias, List<TbDsjRuleEdit> tbDsjRuleEdits, String line) {
        GzipCompressorInputStream gzip = null;
        try {
            gzip = new GzipCompressorInputStream(inputStream);
            String s = readFileToString(gzip);
            // log.error("文件" + s);
            if (StringUtils.hasText(s)) {
                JData jdata = JSON.parseObject(s, JData.class);
                log.info(line+"解析完成:准备写入MongoDB数据库extid："+jdata.getKey().get("extid"));

                Map<String, List<String>> row = jdata.getRow();

                //原始数据处理
                Map<String, List<Double>> map = new HashMap<>();
                //取宝信数据，放入我们内部别名变量中
                if (!CollectionUtils.isEmpty(alias)) {
                    alias.forEach(t -> {
                        String l1_variable_name = t.getL1_variable_name();//宝信变量名
                        String l1_variable_alias = t.getL1_variable_alias();//咱们的变量别名
                            List<String> list = row.get(l1_variable_name);
                            if (!CollectionUtils.isEmpty(list)) {
                                List<Double> collect = list.stream().map(Double::parseDouble).collect(Collectors.toList());
                                map.put(l1_variable_alias, collect);
                            }
                     /*   if (!l1_variable_alias.contains("-")) {
                            List<String> list = row.get(l1_variable_name);


                            if (!CollectionUtils.isEmpty(list)) {
                                List<Double> collect = list.stream().map(Double::parseDouble).collect(Collectors.toList());
                                if (l1_variable_name.equals("C642_CPC4_HC1Pos")) {
                                    // System.out.println(row.get(l1_variable_name).toString());
                                }
                                map.put(l1_variable_alias, collect);
                            }
                        }*/
                    });
                }

                //特征值处理
                HashMap<String, Double> trailValues = new HashMap<>();
                if (!CollectionUtils.isEmpty(tbDsjRuleEdits)) {
                    tbDsjRuleEdits.forEach(t -> {
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("avg")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                          /*  //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }*/
                            list = calUtils.calSubMid(list, t.getHead_move(), t.getTail_move());
                            if (!CollectionUtils.isEmpty(list)) {
                                Double avg = calUtils.calAvg(list);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("max")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            /*//雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }*/
                            list = calUtils.calSubMid(list, t.getHead_move(), t.getTail_move());
                            if (!CollectionUtils.isEmpty(list)) {
                                Double max = calUtils.calMax(list);
                                trailValues.put(t.getCalc_result_variable(), max);
                            }
                        }
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("min")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                          /*  //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }*/
                            list = calUtils.calSubMid(list, t.getHead_move(), t.getTail_move());
                            if (!CollectionUtils.isEmpty(list)) {
                                Double min = calUtils.calMin(list);
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }
                        //极差
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("range")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                          /*  //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }*/
                            list = calUtils.calSubMid(list, t.getHead_move(), t.getTail_move());
                            if (!CollectionUtils.isEmpty(list)) {
                                Double range = calUtils.calRange(list);
                                trailValues.put(t.getCalc_result_variable(), range);
                            }
                        }

                        //标准差
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("std")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                          /*  //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }*/
                            list = calUtils.calSubMid(list, t.getHead_move(), t.getTail_move());
                            if (!CollectionUtils.isEmpty(list)) {
                                Double standard = calUtils.calStandard(list);
                                trailValues.put(t.getCalc_result_variable(), standard);
                            }
                        }

                        //众数，出现最多的数据
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("mode")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                          /*  //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }*/
                            list = calUtils.calSubMid(list, t.getHead_move(), t.getTail_move());
                            if (!CollectionUtils.isEmpty(list)) {
                                Double Frequent = calUtils.findMostFrequentNumber(list);
                                trailValues.put(t.getCalc_result_variable(), Frequent);
                            }
                        }


                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/avg")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double avg = calUtils.calAvg(subSection);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/max")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double max = calUtils.calMax(subSection);
                                trailValues.put(t.getCalc_result_variable(), max);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/min")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double min = calUtils.calMin(subSection);
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("evermax")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            Double max = calUtils.calMax(list); // 最大值
                            Double min = calUtils.calMin(list); // 最小值
                            if (Math.abs(max) >= Math.abs(min)) {
                                trailValues.put(t.getCalc_result_variable(), max);
                            } else {
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/range")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double range = calUtils.calRange(subSection);
                                trailValues.put(t.getCalc_result_variable(), range);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/stdvar")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double standard = calUtils.calStandard(subSection);
                                trailValues.put(t.getCalc_result_variable(), standard);
                            }
                        }

                        // 622特征值
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/avg")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double avg = calUtils.calAvg(subSection);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/max")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double max = calUtils.calMax(subSection);
                                trailValues.put(t.getCalc_result_variable(), max);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/min")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double min = calUtils.calMin(subSection);
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/range")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double range = calUtils.calRange(subSection);
                                trailValues.put(t.getCalc_result_variable(), range);
                            }
                        }


                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/stdvar")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double standard = calUtils.calStandard(subSection);
                                trailValues.put(t.getCalc_result_variable(), standard);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("abs-hs/avg")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            String[] split = t.getDic_variable_name_calc().split("-");
                            List<Double> list1 = map.get(split[0]);
                            List<Double> list2 = map.get(split[1]);

                            List<Double> subSection1 = calUtils.calSubSection(list1, section.get("start"), section.get("stop"));
                            List<Double> subSection2 = calUtils.calSubSection(list2, section.get("start"), section.get("stop"));
                            List<Double> result = calUtils.calListReduce(subSection1, subSection2);
                            if (!CollectionUtils.isEmpty(result)) {
                                Double avg = calUtils.calAvg(result);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("abs-hs/stdvar")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            String[] split = t.getDic_variable_name_calc().split("-");
                            List<Double> list1 = map.get(split[0]);
                            List<Double> list2 = map.get(split[1]);

                            List<Double> subSection1 = calUtils.calSubSection(list1, section.get("start"), section.get("stop"));
                            List<Double> subSection2 = calUtils.calSubSection(list2, section.get("start"), section.get("stop"));
                            List<Double> result = calUtils.calListReduce(subSection1, subSection2);
                            if (!CollectionUtils.isEmpty(result)) {
                                Double standard = calUtils.calStandard(result);
                                trailValues.put(t.getCalc_result_variable(), standard);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("abs-hs/range")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            String[] split = t.getDic_variable_name_calc().split("-");
                            List<Double> list1 = map.get(split[0]);
                            List<Double> list2 = map.get(split[1]);

                            List<Double> subSection1 = calUtils.calSubSection(list1, section.get("start"), section.get("stop"));
                            List<Double> subSection2 = calUtils.calSubSection(list2, section.get("start"), section.get("stop"));
                            List<Double> result = calUtils.calListReduce(subSection1, subSection2);
                            if (!CollectionUtils.isEmpty(result)) {
                                Double range = calUtils.calRange(result);
                                trailValues.put(t.getCalc_result_variable(), range);
                            }
                        }


                    });
                }


                JDataSave621 jDataSave621 = null;
                JDataSave622 jDataSave622 = null;
                JDataSave642 jDataSave642 = null;
                JDataSave653 jDataSave653 = null;

                JDataSavemong1630 JDataSavemong1630 = null;
                JDataSavemong2280_1 JDataSavemong2280_1 = null;
                JDataSavemong2150 JDataSavemong2150 = null;
                JDataSavemong1870 JDataSavemong1870 = null;

                if (StringUtils.hasText(line) && line.equals("CGL1870")) {
                    JDataSavemong1870 = new JDataSavemong1870();
                    JDataSavemong1870.setExtid(jdata.getKey().get("extid"));
                    JDataSavemong1870.setEntid(jdata.getKey().get("entid"));
                    JDataSavemong1870.setStart(jdata.getKey().get("start"));
                    JDataSavemong1870.setEnd(jdata.getKey().get("end"));
                    //存入规则编辑表里所需的特征值
                    JDataSavemong1870.setFlag(1);//等于1为存入特征值
                    JDataSavemong1870.setTrailValues(trailValues);
                    JDataSavemong1870.setCreatetime(new Date());
                    jDataSavemongDao1870.save(JDataSavemong1870);
                    // 遍历 Map-存每个曲线
                    for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
                        //  System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
                        JDataSavemong1870 = new JDataSavemong1870();
                        JDataSavemong1870.setExtid(jdata.getKey().get("extid"));
                        JDataSavemong1870.setEntid(jdata.getKey().get("entid"));
                        JDataSavemong1870.setStart(jdata.getKey().get("start"));
                        JDataSavemong1870.setEnd(jdata.getKey().get("end"));
                        //存入数据字典所需的原始宝信长度数据
                        JDataSavemong1870.setFlag(0);//等于0为存入曲线
                        JDataSavemong1870.setVar_name(entry.getKey());
                        JDataSavemong1870.setCurve(entry.getValue());
                        JDataSavemong1870.setCreatetime(new Date());
                        jDataSavemongDao1870.save(JDataSavemong1870);
                    }
                    log.info("CGL1870写入mongdb>>>extid:"+jdata.getKey().get("extid"));

                }

                if (StringUtils.hasText(line) && line.equals("CAL1630")) {
                    JDataSavemong1630 = new JDataSavemong1630();
                    JDataSavemong1630.setExtid(jdata.getKey().get("extid"));
                    JDataSavemong1630.setEntid(jdata.getKey().get("entid"));
                    JDataSavemong1630.setStart(jdata.getKey().get("start"));
                    JDataSavemong1630.setEnd(jdata.getKey().get("end"));
                    //存入规则编辑表里所需的特征值
                    JDataSavemong1630.setFlag(1);//等于1为存入特征值
                    JDataSavemong1630.setTrailValues(trailValues);
                    JDataSavemong1630.setCreatetime(new Date());
                    jDataSavemongDao1630.save(JDataSavemong1630);
                    // 遍历 Map-存每个曲线
                    for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
                        //  System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
                        JDataSavemong1630 = new JDataSavemong1630();
                        JDataSavemong1630.setExtid(jdata.getKey().get("extid"));
                        JDataSavemong1630.setEntid(jdata.getKey().get("entid"));
                        JDataSavemong1630.setStart(jdata.getKey().get("start"));
                        JDataSavemong1630.setEnd(jdata.getKey().get("end"));
                        //存入数据字典所需的原始宝信长度数据
                        //  JDataSavemong1630.setRow(map);
                        JDataSavemong1630.setFlag(0);//等于0为存入曲线
                        JDataSavemong1630.setVar_name(entry.getKey());
                        JDataSavemong1630.setCurve(entry.getValue());
                        //存入规则编辑表里所需的特征值
                        // JDataSavemong1630.setTrailValues(trailValues);
                        JDataSavemong1630.setCreatetime(new Date());
                        jDataSavemongDao1630.save(JDataSavemong1630);
                    }
                    log.info("CAL1630写入mongdb>>>extid:"+jdata.getKey().get("extid"));

                }

                if (StringUtils.hasText(line) && line.equals("TCM2280")) {
                  //存特征值
                    JDataSavemong2280_1   JDataSavemong2280_1_a = new JDataSavemong2280_1();
                    JDataSavemong2280_1_a.setExtid(jdata.getKey().get("extid"));
                    JDataSavemong2280_1_a.setEntid(jdata.getKey().get("entid"));
                    JDataSavemong2280_1_a.setStart(jdata.getKey().get("start"));
                    JDataSavemong2280_1_a.setEnd(jdata.getKey().get("end"));
                    JDataSavemong2280_1_a.setTrailValues(trailValues);
                    JDataSavemong2280_1_a.setCreatetime(new Date());
                    //存入规则编辑表里所需的特征值
                    JDataSavemong2280_1_a.setFlag(1);//等于1为存入特征值
                     JDataSavemong2280_1_a.setTrailValues(trailValues);
                    jDataSavemongDao2280.save(JDataSavemong2280_1_a);

                    // 遍历 Map-存每个曲线
                    for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
                      //  System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
                        JDataSavemong2280_1 = new JDataSavemong2280_1();
                        JDataSavemong2280_1.setExtid(jdata.getKey().get("extid"));
                        JDataSavemong2280_1.setEntid(jdata.getKey().get("entid"));
                        JDataSavemong2280_1.setStart(jdata.getKey().get("start"));
                        JDataSavemong2280_1.setEnd(jdata.getKey().get("end"));
                        //存入数据字典所需的原始宝信长度数据
                      //  JDataSavemong2280.setRow(map);
                        JDataSavemong2280_1.setFlag(0);//等于0为存入曲线
                        JDataSavemong2280_1.setVar_name(entry.getKey());
                        JDataSavemong2280_1.setCurve(entry.getValue());
                        //存入规则编辑表里所需的特征值
                       // JDataSavemong2280_1.setTrailValues(trailValues);
                        JDataSavemong2280_1.setCreatetime(new Date());
                        jDataSavemongDao2280.save(JDataSavemong2280_1);

                    }
                    log.info("TCM2280写入mongdb>>>extid:"+jdata.getKey().get("extid"));

                }

                if (StringUtils.hasText(line) && line.equals("CAL2150")) {
                    JDataSavemong2150 = new JDataSavemong2150();
                    JDataSavemong2150.setExtid(jdata.getKey().get("extid"));
                    JDataSavemong2150.setEntid(jdata.getKey().get("entid"));
                    JDataSavemong2150.setStart(jdata.getKey().get("start"));
                    JDataSavemong2150.setEnd(jdata.getKey().get("end"));
                    //存入规则编辑表里所需的特征值
                    JDataSavemong2150.setFlag(1);//等于1为存入特征值
                    JDataSavemong2150.setTrailValues(trailValues);
                    JDataSavemong2150.setCreatetime(new Date());
                    jDataSavemongDao2150.save(JDataSavemong2150);
                    // 遍历 Map-存每个曲线
                    for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
                        //  System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
                        JDataSavemong2150 = new JDataSavemong2150();
                        JDataSavemong2150.setExtid(jdata.getKey().get("extid"));
                        JDataSavemong2150.setEntid(jdata.getKey().get("entid"));
                        JDataSavemong2150.setStart(jdata.getKey().get("start"));
                        JDataSavemong2150.setEnd(jdata.getKey().get("end"));
                        //存入数据字典所需的原始宝信长度数据
                        JDataSavemong2150.setFlag(0);//等于0为存入曲线
                        JDataSavemong2150.setVar_name(entry.getKey());
                        JDataSavemong2150.setCurve(entry.getValue());
                        //存入规则编辑表里所需的特征值
                        JDataSavemong2150.setCreatetime(new Date());
                        jDataSavemongDao2150.save(JDataSavemong2150);

                    }
                    log.info("CAL2150写入mongdb>>>extid:"+jdata.getKey().get("extid"));
                }


               /* if (StringUtils.hasText(line) && line.equals("621")) {
                    jDataSave621 = new JDataSave621();
                    jDataSave621.setExtid(jdata.getKey().get("extid"));
                    jDataSave621.setEntid(jdata.getKey().get("entid"));
                    jDataSave621.setStart(jdata.getKey().get("start"));
                    jDataSave621.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave621.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave621.setTrailValues(trailValues);
                    jDataSave621.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>621");
                    jDataSaveService621.insertData(jDataSave621);
                }
                if (StringUtils.hasText(line) && line.equals("622")) {
                    jDataSave622 = new JDataSave622();
                    jDataSave622.setExtid(jdata.getKey().get("extid"));
                    jDataSave622.setEntid(jdata.getKey().get("entid"));
                    jDataSave622.setStart(jdata.getKey().get("start"));
                    jDataSave622.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave622.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave622.setTrailValues(trailValues);
                    jDataSave622.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>622");
                    jDataSaveService622.insertData(jDataSave622);
                }
                if (StringUtils.hasText(line) && line.equals("642")) {
                    jDataSave642 = new JDataSave642();
                    jDataSave642.setExtid(jdata.getKey().get("extid"));
                    jDataSave642.setEntid(jdata.getKey().get("entid"));
                    jDataSave642.setStart(jdata.getKey().get("start"));
                    jDataSave642.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave642.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave642.setTrailValues(trailValues);
                    jDataSave642.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>642");

                    jDataSaveService642.insertData(jDataSave642);
                }
                if (StringUtils.hasText(line) && line.equals("653")) {
                    jDataSave653 = new JDataSave653();
                    jDataSave653.setExtid(jdata.getKey().get("extid"));
                    jDataSave653.setEntid(jdata.getKey().get("entid"));
                    jDataSave653.setStart(jdata.getKey().get("start"));
                    jDataSave653.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave653.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave653.setTrailValues(trailValues);
                    jDataSave653.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>653");

                    jDataSaveService653.insertData(jDataSave653);
                }*/
            }

        } catch (IOException e) {
            log.error("写入MongoDB数据库失败,失败信息为" + e);
            e.printStackTrace();
        } finally {
            try {
                if (gzip != null) {
                    gzip.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




    /**
     * 将GZip文件内容写入MongoDB
     *
     * @param inputStream 需要进行json解析的GZip压缩文件流
     */
    //@Async("executor")
    public void readFileToMongoDB(InputStream inputStream, List<TbDsjDataCollect> alias, List<TbDsjRuleEdit> tbDsjRuleEdits, String line) {
        GzipCompressorInputStream gzip = null;
        try {
            gzip = new GzipCompressorInputStream(inputStream);
            String s = readFileToString(gzip);
            // log.error("文件" + s);
            if (StringUtils.hasText(s)) {
                JData jdata = JSON.parseObject(s, JData.class);
                log.error("解析完成:准备写入MongoDB数据库");

                Map<String, List<String>> row = jdata.getRow();

                //原始数据处理
                Map<String, List<Double>> map = new HashMap<>();
                //取宝信数据，放入我们内部别名变量中
                if (!CollectionUtils.isEmpty(alias)) {
                    alias.forEach(t -> {
                        String l1_variable_name = t.getL1_variable_name();
                        String l1_variable_alias = t.getL1_variable_alias();
                        if (!l1_variable_alias.contains("-")) {
                            List<String> list = row.get(l1_variable_name);


                            if (!CollectionUtils.isEmpty(list)) {
                                List<Double> collect = list.stream().map(Double::parseDouble).collect(Collectors.toList());
                                if (l1_variable_name.equals("C642_CPC4_HC1Pos")) {
                                   // System.out.println(row.get(l1_variable_name).toString());
                                }
                                map.put(l1_variable_alias, collect);
                            }
                        }
                    });
                }

                //特征值处理
                HashMap<String, Double> trailValues = new HashMap<>();
                if (!CollectionUtils.isEmpty(tbDsjRuleEdits)) {
                    tbDsjRuleEdits.forEach(t -> {
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("avg")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }
                            if (!CollectionUtils.isEmpty(list)) {
                                Double avg = calUtils.calAvg(list);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("max")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }
                            if (!CollectionUtils.isEmpty(list)) {
                                Double max = calUtils.calMax(list);
                                trailValues.put(t.getCalc_result_variable(), max);
                            }
                        }
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("min")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            //雨田截取头部尾部中部
                            if (StringUtils.hasText(t.getData_begin()) && !t.getData_begin().equals(0)) {
                                TbDsjRuleEditRelation relation = tbDsjRuleEditRelationDao.findDataTbDsjRuleEditRelationByIndocno(Long.valueOf(t.getData_begin()));
                                if (t.getData_begin().equals("1")) {
                                    list = calUtils.calSubHead(list, relation.getRule_edit_value().intValue());
                                }
                                if (t.getData_begin().equals("2")) {
                                    list = calUtils.calSubMid(list, relation.getRule_edit_value().intValue(), relation.getRule_edit_value2().intValue());
                                }
                                if (t.getData_begin().equals("3")) {
                                    list = calUtils.calSubTail(list, relation.getRule_edit_value2().intValue());
                                }
                            }
                            if (!CollectionUtils.isEmpty(list)) {
                                Double min = calUtils.calMin(list);
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/avg")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double avg = calUtils.calAvg(subSection);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/max")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double max = calUtils.calMax(subSection);
                                trailValues.put(t.getCalc_result_variable(), max);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/min")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double min = calUtils.calMin(subSection);
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("evermax")) {
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            Double max = calUtils.calMax(list); // 最大值
                            Double min = calUtils.calMin(list); // 最小值
                            if (Math.abs(max) >= Math.abs(min)) {
                                trailValues.put(t.getCalc_result_variable(), max);
                            } else {
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/range")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double range = calUtils.calRange(subSection);
                                trailValues.put(t.getCalc_result_variable(), range);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("highspeed/stdvar")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double standard = calUtils.calStandard(subSection);
                                trailValues.put(t.getCalc_result_variable(), standard);
                            }
                        }

                        // 622特征值
                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/avg")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double avg = calUtils.calAvg(subSection);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/max")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double max = calUtils.calMax(subSection);
                                trailValues.put(t.getCalc_result_variable(), max);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/min")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double min = calUtils.calMin(subSection);
                                trailValues.put(t.getCalc_result_variable(), min);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/range")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double range = calUtils.calRange(subSection);
                                trailValues.put(t.getCalc_result_variable(), range);
                            }
                        }


                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("c622highspeed/stdvar")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c622_s5_speed"));
                            List<Double> list = map.get(t.getDic_variable_name_calc());
                            List<Double> subSection = calUtils.calSubSection(list, section.get("start"), section.get("stop"));
                            if (!CollectionUtils.isEmpty(subSection)) {
                                Double standard = calUtils.calStandard(subSection);
                                trailValues.put(t.getCalc_result_variable(), standard);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("abs-hs/avg")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            String[] split = t.getDic_variable_name_calc().split("-");
                            List<Double> list1 = map.get(split[0]);
                            List<Double> list2 = map.get(split[1]);

                            List<Double> subSection1 = calUtils.calSubSection(list1, section.get("start"), section.get("stop"));
                            List<Double> subSection2 = calUtils.calSubSection(list2, section.get("start"), section.get("stop"));
                            List<Double> result = calUtils.calListReduce(subSection1, subSection2);
                            if (!CollectionUtils.isEmpty(result)) {
                                Double avg = calUtils.calAvg(result);
                                trailValues.put(t.getCalc_result_variable(), avg);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("abs-hs/stdvar")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            String[] split = t.getDic_variable_name_calc().split("-");
                            List<Double> list1 = map.get(split[0]);
                            List<Double> list2 = map.get(split[1]);

                            List<Double> subSection1 = calUtils.calSubSection(list1, section.get("start"), section.get("stop"));
                            List<Double> subSection2 = calUtils.calSubSection(list2, section.get("start"), section.get("stop"));
                            List<Double> result = calUtils.calListReduce(subSection1, subSection2);
                            if (!CollectionUtils.isEmpty(result)) {
                                Double standard = calUtils.calStandard(result);
                                trailValues.put(t.getCalc_result_variable(), standard);
                            }
                        }

                        if (StringUtils.hasText(t.getRule_editor()) && t.getRule_editor().equals("abs-hs/range")) {
                            Map<String, Integer> section = calUtils.calHighLowSection(map.get("c621_tcm_speed_s4_rolling_act"));
                            String[] split = t.getDic_variable_name_calc().split("-");
                            List<Double> list1 = map.get(split[0]);
                            List<Double> list2 = map.get(split[1]);

                            List<Double> subSection1 = calUtils.calSubSection(list1, section.get("start"), section.get("stop"));
                            List<Double> subSection2 = calUtils.calSubSection(list2, section.get("start"), section.get("stop"));
                            List<Double> result = calUtils.calListReduce(subSection1, subSection2);
                            if (!CollectionUtils.isEmpty(result)) {
                                Double range = calUtils.calRange(result);
                                trailValues.put(t.getCalc_result_variable(), range);
                            }
                        }


                    });
                }


                JDataSave621 jDataSave621 = null;
                JDataSave622 jDataSave622 = null;
                JDataSave642 jDataSave642 = null;
                JDataSave653 jDataSave653 = null;

                if (StringUtils.hasText(line) && line.equals("621")) {
                    jDataSave621 = new JDataSave621();
                    jDataSave621.setExtid(jdata.getKey().get("extid"));
                    jDataSave621.setEntid(jdata.getKey().get("entid"));
                    jDataSave621.setStart(jdata.getKey().get("start"));
                    jDataSave621.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave621.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave621.setTrailValues(trailValues);
                    jDataSave621.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>621");
                    jDataSaveService621.insertData(jDataSave621);
                }
                if (StringUtils.hasText(line) && line.equals("622")) {
                    jDataSave622 = new JDataSave622();
                    jDataSave622.setExtid(jdata.getKey().get("extid"));
                    jDataSave622.setEntid(jdata.getKey().get("entid"));
                    jDataSave622.setStart(jdata.getKey().get("start"));
                    jDataSave622.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave622.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave622.setTrailValues(trailValues);
                    jDataSave622.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>622");
                    jDataSaveService622.insertData(jDataSave622);
                }
                if (StringUtils.hasText(line) && line.equals("642")) {
                    jDataSave642 = new JDataSave642();
                    jDataSave642.setExtid(jdata.getKey().get("extid"));
                    jDataSave642.setEntid(jdata.getKey().get("entid"));
                    jDataSave642.setStart(jdata.getKey().get("start"));
                    jDataSave642.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave642.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave642.setTrailValues(trailValues);
                    jDataSave642.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>642");

                    jDataSaveService642.insertData(jDataSave642);
                }
                if (StringUtils.hasText(line) && line.equals("653")) {
                    jDataSave653 = new JDataSave653();
                    jDataSave653.setExtid(jdata.getKey().get("extid"));
                    jDataSave653.setEntid(jdata.getKey().get("entid"));
                    jDataSave653.setStart(jdata.getKey().get("start"));
                    jDataSave653.setEnd(jdata.getKey().get("end"));
                    //存入数据字典所需的原始宝信长度数据
                    jDataSave653.setRow(map);
                    //存入规则编辑表里所需的特征值
                    jDataSave653.setTrailValues(trailValues);
                    jDataSave653.setCreatetime(new Date());
                    log.error("准备写入mongdb>>>653");

                    jDataSaveService653.insertData(jDataSave653);
                }
            }

        } catch (IOException e) {
            log.error("C642写入MongoDB数据库失败,失败信息为" + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (gzip != null) {
                    gzip.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 将文件解析成字符串
     *
     * @param inputStream 需要解析的文件流
     * @return
     */
    public String readFileToString(InputStream inputStream) {
        try {
            StringBuffer result = new StringBuffer();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while ((line = br.readLine()) != null) {
                result.append(line.trim());
            }
            br.close();
            inputStream.close();
            return result.toString().trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文件下载到本地
     *
     * @param inputStream 需要下载到本地的文件流
     * @param loadPath    本地存放文件的地址
     */
    public void loadFile(InputStream inputStream, String loadPath) {
        //将InputStream转File
        try {
            FileOutputStream outputStream = new FileOutputStream(loadPath);
            int length = 0;
            byte[] b = new byte[1024];
            while ((length = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, length);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件下载失败");
        }
    }

    public void test_ben(String directory, String downloadFile, String host) throws SftpException, IOException, JSchException {

        ChannelSftp sftp = sftpConnect(host);

        if (directory != null && !"".equals(directory)) {
            sftp.cd(directory);
        }
        InputStream inputStream = sftp.get(directory + downloadFile);
        //判断文件大小
        loadFile(inputStream, "D://TEMP/" + downloadFile);
        File file = new File("D://TEMP/" + downloadFile);

        if (file.length() > 1024 * 1024) {
            log.error(directory + downloadFile + "653文件大小异常---" + host);
            inputStream.close();
            file.delete();
            sftp.disconnect();
            sftp.getSession().disconnect();
        }
        file.delete();
        //读完再读一次
        InputStream inputStream1 = sftp.get(directory + downloadFile);
        if (inputStream1 != null) {
            GzipCompressorInputStream gzip = null;
            gzip = new GzipCompressorInputStream(inputStream1);
            String s = readFileToString(gzip);
            if (StringUtils.hasText(s)) {
                JData jdata = JSON.parseObject(s, JData.class);
                log.info("解析1");
                Map<String, List<String>> row = jdata.getRow();
                log.info("解析2");
            }

        }
        inputStream1.close();
        sftp.disconnect();
        sftp.getSession().disconnect();

    }
}
